package com.healthtop.web;

import com.healthtop.common.Page;
import com.healthtop.common.Views;
import com.healthtop.dto.*;
import com.healthtop.model.XsUser;
import com.healthtop.service.DIYService;
import com.healthtop.service.XsAdviceService;
import com.healthtop.service.XsGameDetailsService;
import com.healthtop.service.XsUserService;
import com.healthtop.utils.AjaxResponseStatus;
import com.healthtop.utils.UserUtils;
import com.healthtop.utils.VerifyCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

@Controller
@RequestMapping("/sites")
public class SitesController {

    @Autowired
    private DIYService diyService;

    @Autowired
    private XsAdviceService xsAdviceService;

    @Autowired
    private XsUserService xsUserService;

    @Autowired
    private XsGameDetailsService xsGameDetailsService;

    /**
     * 进入网站首页
     * @return
     */
    @RequestMapping(value="/")
    public String homePage(){
        return Views.FORWARD_SITES_HOME_PAGE;
    }

    /**
     *进入网站首页
     * @param request
     * @param response
     * @param sitesDto
     * @param page
     * @return
     */
    @RequestMapping(value = "/homePage")
    public String index(HttpServletRequest request, HttpServletResponse response, SitesDto sitesDto, Page page){
        diyService.getSitesAll(request,response,sitesDto,page);
        return Views.HOME_PAGE;
    }

    /**
     * 添加意见
     * @param xsAdviceDto
     * @return
     */
    @RequestMapping(value = "/addAdvice")
    @ResponseBody
    public AjaxResponseStatus addAdvice(XsAdviceDto xsAdviceDto){
        return xsAdviceService.addAdvice(xsAdviceDto);
    }

    /**
     * 玩法指导
     * @param sitesDto
     * @return
     */
    @RequestMapping("/forwardGameDetail/list")
    public String forwardGameDetail(SitesDto sitesDto,Page page){
        if(!ObjectUtils.isEmpty(UserUtils.getCurrentUser())){
            diyService.forwardGameDetail(sitesDto,page);
            return Views.STYLE_GUIDE;
        }else{
           return Views.REDIRECT_SITES_LOGIN;
        }

    }


    /**
     * 进入网站的登陆
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login(){
        return Views.SITES_LOGIN;
    }


    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(LoginDto loginDto, HttpServletRequest request){
        XsUser user = xsUserService.login(loginDto,request);
        if(loginDto.isError()){
            return Views.SITES_LOGIN;
        }else{
//            List<MenuDto> menus = navigationService.getMenus(user.getUserId());
//            UserUtils.setMenus(menus);
            return Views.FORWARD_SITES_HOME_PAGE;
        }
    }


    @RequestMapping(value="/logout")
    public String logout(){
        UserUtils.cleanSession();
        return Views.REDIRECT_SITES_LOGIN;
    }


    /* 获取验证码图片*/
    @RequestMapping(value="/getVerifyCode")
    public void getVerificationCode(HttpServletResponse response, HttpServletRequest request) {

        try {

            int width=200;

            int height=69;

            BufferedImage verifyImg=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

//生成对应宽高的初始图片

            String randomText = VerifyCode.drawRandomText(width,height,verifyImg);

//单独的一个类方法，出于代码复用考虑，进行了封装。

//功能是生成验证码字符并加上噪点，干扰线，返回值为验证码字符

            request.getSession().setAttribute("verifyCode", randomText);

            response.setContentType("image/png");//必须设置响应内容类型为图片，否则前台不识别

            OutputStream os = response.getOutputStream(); //获取文件输出流

            ImageIO.write(verifyImg,"png",os);//输出图片流

            os.flush();

            os.close();//关闭流

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

    @RequestMapping(value="/toRegister")
    public String toRegister(RegisteredDto registeredDto){
        return Views.SITES_TO_REGISTER;
    }

    @RequestMapping(value = "/registered",method = RequestMethod.POST)
    public String login(RegisteredDto registeredDto, HttpServletRequest request){
        XsUser user = xsUserService.registered(registeredDto,request);
        if(registeredDto.isError()){
            return Views.FORWARD_SITES_TO_REGISTER;
        }else{
//            List<MenuDto> menus = navigationService.getMenus(user.getUserId());
//            UserUtils.setMenus(menus);
            return Views.FORWARD_SITES_HOME_PAGE;
        }
    }

    /**
     * 进入游戏详情文章页
     * @param xsGameDetailsDto
     * @return
     */
    @RequestMapping("/styleGuideDetail")
    public String styleGuideDetail(XsGameDetailsDto xsGameDetailsDto){
        if(!ObjectUtils.isEmpty(UserUtils.getCurrentUser())){
            xsGameDetailsService.styleGuideDetail(xsGameDetailsDto);
            return Views.STYLE_GUIDE_DETAIL;
        }else{
            return Views.REDIRECT_SITES_LOGIN;
        }

    }
}
